<template>
 <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <!-- <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.name"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item> -->
	  <el-form-item label="部门名称">
	    <el-input v-model="searchForm.deptName" placeholder="部门名称" clearable/>
	  </el-form-item>
	  <el-form-item label="岗位名称">
	    <el-input
	      v-model="searchForm.positionName"
	      placeholder="岗位名称"
	      clearable
	    ></el-input>
	  </el-form-item>
	  <el-form-item label="岗位考核条目">
	    <el-input
	      v-model="searchForm.target"
	      placeholder="岗位考核条目"
	      clearable
	    ></el-input>
	  </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
        <el-date-picker
          v-model="searchForm.time"
          type="month"
          placeholder="选择月"
        		value-format="YYYY-MM"
        		:disabledDate="check">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-table
    :data="PerformanceRuleList"
    border
    style="width: 95%; margin: 20px 0px; border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :span-method="(value) => rowSpanRulesHandler(value, PerformanceRuleList)"
  >
  
  
<el-table-column label="部门/岗位" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.deptName }}/{{ scope.row.position }}</span>
      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="岗位考核条目" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.target2 == null ? "仅用于评分结算":scope.row.target2}}</span>
      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="计算方式" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.type }}</span>
      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="关联部门具体考核指标" width="210">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.deptNamePerformance}}/{{scope.row.type}}/{{scope.row.target}}</span>
        </div>
      </template>
    </el-table-column>
	
	<el-table-column prop="budget" label="预算值" width=""></el-table-column>
	<el-table-column prop="complete" label="完成值" width=""></el-table-column>
	
	<el-table-column label="实际完成进度" width="">
		<template #default="scope">
		  <div style="display: flex; align-items: center">
		    <span style="margin-left: 10px">{{ scope.row.grade1}}%</span>
		  </div>
		</template>
	</el-table-column>
	
	
	<el-table-column prop="grade2" label="转换得分" width=""></el-table-column>
	
	<el-table-column prop="settlement" label="是否应用评分结算" width="">
		<template #default="scope">
		  <div style="display: flex; align-items: center">
		    <span style="margin-left: 10px">{{ scope.row.settlement == 0?"否":"是"}}</span>
		  </div>
		</template>
	</el-table-column>
	
	<el-table-column prop="ins" label="备注"/>
	
	
	
	<el-table-column label="最后更新时间" width="200">
	  <template #default="scope">
	    <div style="display: flex; align-items: center">
	      <el-icon><timer/></el-icon>
	      <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
	    </div>
	  </template>
	</el-table-column>
	
	
  </el-table>
  <pagination
    class="pagination"
    v-show="PerformanceListPage.total > 0"
    :total="PerformanceListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="PerformanceListPage.size"
    @pagination="handlePagination"
  />
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getPerformanceRulePastPage,getPerformanceRulePastSearch } from "@/api/positionPerformance";
import { ElMessage } from "element-plus";

const betweenTime=ref(null)
const searchForm = ref({
  deptName:"",
  positionName:"",
  target:"",
  page: 1,
  page_size: 10,
  time:"",
});
const PerformanceListPage = ref([]);
const PerformanceRuleList = ref([]);
onBeforeMount(() => {
  getPerformanceRulePastPage(searchForm.value).then((data) => {
    PerformanceListPage.value = data.data;
    PerformanceRuleList.value = data.data.records;
  });
});
watch(
  PerformanceRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const handlePagination=(value)=>{
  if(searchForm.value.name==""&&searchForm.value.betweenTime==""){
    getPerformanceRulePastPage(value).then(data=>{
        PerformanceListPage.value = data.data;
		PerformanceRuleList.value = data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getPerformanceRulePastSearch(searchForm.value).then(data=>{
		PerformanceListPage.value = data.data;
		PerformanceRuleList.value = data.data.records;
    })
  } 
}

const searchEvent=()=>{
  getPerformanceRulePastSearch(searchForm.value).then(data=>{
		PerformanceListPage.value = data.data;
		PerformanceRuleList.value = data.data.records;
    })
}

const update = (form) => {
  searchForm.value.deptName=""
  searchForm.value.positionName=""
  searchForm.value.target=""
  getPerformanceRulePastPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "刷新成功",
        type: "success",
      });
      PerformanceListPage.value = data.data;
      PerformanceRuleList.value = data.data.records;
    } else {
      ElMessage.error("刷新失败");
    }
  });
};

const compareSingleAttrReturnColSpan = (row, compareAttr, index, tableData) => {
  let count = 0;
  let start = index;
  let multCompare = true;

  while (start < tableData.length && multCompare) {
    start++;
    count++;
    if (start <= tableData.length - 1)
      multCompare = row.id === tableData[start].id;
    else multCompare = row.id === tableData[start - 1].id;
  }
  start--; 

  if (start < tableData.length) {
    tableData[index][`${compareAttr}_count`] = count; 
  }

  if (index > 0 && count + 1 === tableData[index - 1][`${compareAttr}_count`]) {
    return [0, 0]; 
  }
  return [count, 1];
};

const columns = [
  "name",
  "target1",
  "target2",
  "state",
  "updateTime",
  "ins",
  "operate",
];
const rowSpanRulesHandler = ({ row, column, rowIndex, columnIndex }, data) => {
  if (
    columns[columnIndex] === "name" ||
    columns[columnIndex] === "ins" ||
    columns[columnIndex] === "operate"
  ) {
    const r = compareSingleAttrReturnColSpan(row, "emp_id", rowIndex, data);
    return r;
  }
};



const now = new Date();
const year = now.getFullYear();
// 获取当前月份（注意：月份从0开始，所以需要加1）
const month = now.getMonth() == 0? 12:now.getMonth();
// // 格式化为 YYYY-MM 形式
const yearMonth = `${year}-${month < 10 ? '0' : ''}${month}`;

searchForm.value.time = yearMonth

const check = (time)=>{
	 return time.getTime() > new Date(new Date().toLocaleDateString()).getTime() - 31 * 24 * 3600 * 1000;
}
watch(searchForm.value, (newValue, oldValue) => {
	searchEvent()
}, { deep: true });

</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>